﻿using System;
using System.Collections.Generic;
using CYZHAPI.Common.DB;
using CYZHAPI.Entity;
using CYZHAPI.IRepository;
using Oracle.ManagedDataAccess.Client;
using System.Data;

namespace CYZHAPI.Repository
{
    public class UserLoginRep : ReponsitoryBase<UserModel>, IUserLoginRep
    {

        public UserLoginRep(IDapperContext dbContext) : base(dbContext)
        {
            
        }

        public UserModel get(string sName, string sPassword)
        {
            UserModel objUser = new UserModel();
            string sSql = "select id, yhm, xm, mm, photo,email ,ustate from SYSUSER where yhm=:yhm and mm=:mm";
            OracleConnection objConn = this.GetReadingConnection();
            using (OracleCommand cmd = objConn.CreateCommand())
            {
                try
                {
                    cmd.CommandText = sSql;
                    // Assign id to the department number 50 
                    OracleParameter yhm = new OracleParameter("yhm", sName);
                    cmd.Parameters.Add(yhm);
                    OracleParameter mm = new OracleParameter("mm", sPassword);
                    cmd.Parameters.Add(mm);
                    //cmd.BindByName = true;
                    DataSet dtTmp = new DataSet();

                    using(OracleDataAdapter dataAdapter = new OracleDataAdapter())
                    {
                        dataAdapter.SelectCommand = cmd;//检索command设置
                        dataAdapter.Fill(dtTmp);//检索结果保存在dtTmp数据集中
                        if (dtTmp.Tables[0].Rows.Count == 1)
                        {//"select id, yhm, xm, mm, photo,email ,ustate from SYSUSER where yhm=:yhm, mm=:mm";
                            objUser.id = dtTmp.Tables[0].Rows[0][0].ToString();
                            objUser.yhm = dtTmp.Tables[0].Rows[0][1].ToString();
                            objUser.xm = dtTmp.Tables[0].Rows[0][2].ToString();
                            objUser.mm = dtTmp.Tables[0].Rows[0][3].ToString();
                            objUser.photo = dtTmp.Tables[0].Rows[0][4].ToString();
                            objUser.email = dtTmp.Tables[0].Rows[0][5].ToString();
                            objUser.ustate = dtTmp.Tables[0].Rows[0][6].ToString();
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
                
            }
            //Oracle.ManagedDataAccess.Client.
            
            return objUser;
        }

        public List<string> getRol(string sName)
        {
            List<string> objList = new List<string>();
            string sSql = "select ROLID from USER_MAP_ROL where USERID=:USERID";
            OracleConnection objConn = this.GetReadingConnection();
            using (OracleCommand cmd = objConn.CreateCommand())
            {
                try
                {
                    cmd.CommandText = sSql;
                    // Assign id to the department number 50 
                    OracleParameter yhm = new OracleParameter("USERID", sName);
                    cmd.Parameters.Add(yhm);
                    
                    //cmd.BindByName = true;
                    DataSet dtTmp = new DataSet();

                    using(OracleDataAdapter dataAdapter = new OracleDataAdapter())
                    {
                        dataAdapter.SelectCommand = cmd;//检索command设置
                        dataAdapter.Fill(dtTmp);//检索结果保存在dtTmp数据集中
                        foreach(DataRow dr in dtTmp.Tables[0].Rows)   ///遍历所有的行
                        {//"select id, yhm, xm, mm, photo,email ,ustate from SYSUSER where yhm=:yhm, mm=:mm";
                            string UserId = dr[0].ToString();
                            objList.Add(UserId);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return null;
                }
                
            }
            return objList;
        }
    }
}